package org.net.device.tools;
import java.io.IOException;
import java.util.*;
import java.net.InetAddress;
import java.text.SimpleDateFormat;

import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class devicetime {
	private String ymd;
	private String hms;
	private int days;
	private int hours;
	private int mins;
	private int secs;
	private long totalsecs;

	
	public devicetime() {
		super();
	}
	
	public devicetime(String time) {
		super();
		setTimes(time);
	}
	
	public static long getMiniSecs(String s) {
		int pos = s.indexOf(":");
		String s1 = s.substring(0, pos);
		long sl1 = Long.parseLong(s1);
		String s2 = s.substring(pos+1);
		long sl2 = Long.parseLong(s2);
		return sl1*3600000+sl2*60000;
	}
	
	public static final boolean isSameDay(long secs1,long secs2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new  Date();
		d.setTime(secs1);
		String ds1 = df.format(d);
		d.setTime(secs2);
		String ds2 = df.format(d);
		if (ds1.equals(ds2)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	public void setSecs(long secs) {
		int day = 24*3600;
		int hour = 3600;
		int min = 60;
		long sec = secs;
		this.totalsecs = secs;
		this.days = Integer.parseInt(String.valueOf((sec/day)));
		sec = sec - ((secs/day)*day);
		this.hours = Integer.parseInt(String.valueOf((sec/hour)));
		sec = sec - ((sec/hour)*hour);
		this.mins = Integer.parseInt(String.valueOf((sec/min)));
		sec = sec - ((sec/min)*min);
		this.secs = Integer.parseInt(String.valueOf(sec));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new  Date();
		d.setTime(secs);
		this.setYmd(df.format(d));
		SimpleDateFormat dd = new SimpleDateFormat("HH:mm:ss");
		this.setHms(dd.format(d));
	}


	public void setTimes(String time) {
		int pos = time.indexOf(" days,");
		if (pos > 0) {
			days = Integer.parseInt(time.substring(0,pos));
			time = time.substring(time.indexOf(", ")+2);
		}
		else {
			days = 0;
		}
		
		
		pos = time.indexOf(":");
		hours = Integer.parseInt(time.substring(0,pos));
		time = time.substring(pos+1);

		pos = time.indexOf(":");
		mins = Integer.parseInt(time.substring(0,pos));
		time = time.substring(pos+1);

		pos = time.indexOf(".");
		secs = Integer.parseInt(time.substring(0,pos));
	}
	
	
	public int getDays() {
		return days;
	}


	public void setDays(int days) {
		this.days = days;
	}


	public int getHours() {
		return hours;
	}


	public void setHours(int hours) {
		this.hours = hours;
	}


	public int getMins() {
		return mins;
	}


	public void setMins(int mins) {
		this.mins = mins;
	}


	public int getSecs() {
		return secs;
	}


	public void setSecs(int secs) {
		this.secs = secs;
	}


	public long getTotalsecs() {
		return days*24*3600+hours*3600+mins*60+secs;
	}
	
	public long getTotalMissecs() {
		return getTotalsecs()*1000;
	}


	public void setTotalsecs(long totalsecs) {
		this.totalsecs = totalsecs;
	}

	public String getHms() {
		return hms;
	}

	public void setHms(String hms) {
		this.hms = hms;
	}

	public String getYmd() {
		return ymd;
	}

	public void setYmd(String ymd) {
		this.ymd = ymd;
	}

}
